
package quicksort;
import java.util.*;
public class Quicksort {

    public static void main(String[] args) {
        
        int x[]=new int [4]; 
        int i;
        Scanner k=new Scanner (System.in);
        
        for (i = 0; i <x.length ; i++) {
            System.out.println("Digite o "+(i+1)+"ºnúmero.");
            x[i]=k.nextInt();
                        
        }
        quicksort(x,0,x.length - 1);
        
        System.out.println("O VETOR ESTÁ ORDENADO");
        for (i = 0; i < x.length; i++) {
            System.out.print(x[i]+" ");
        }
 
    }
    public static void troca (int x[],int i,int j){
    int aux;
    aux=x[i];
    x[i]=x[j];
    x[j]=aux;
    }
    public static int particao(int x[],int p,int r){
    
    int pivo,i,j;
    pivo=x[(p+r)/2];
    i=p-1;
    j=r+1;
    while(i<j){
    do{
        
        j=j-1;
    }
    while(x[j]>pivo);
    do{
    i=i+1;
    }
    while(x[i]<pivo);
    if(i<j)troca(x,i,j);
    }
    return j; 
    }
    public static void quicksort(int x[],int p,int r){
    
        int q;
        if(p<r){
        q=particao(x,p,r);
        quicksort(x,p,q);
        quicksort(x,q+1,r);
        
        } 
    } 
}
